<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2024/1/4
 * Time: 14:05
 */

namespace app\api\command\spider;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\config;
use think\Db;
use QL\QueryList;
use QL\Ext\Chrome;

class Baidu extends SpiderBase
{
    private $baseDomain;
    private $createHtmlRootDir;
    private $ql;
    protected function configure()
    {
        $this->setName('baidu')              		//定义命令的名字
        ->setDescription('baidu')      //定义命令的描述
        ->addArgument('method')                   //参数1
        ->addArgument('id');                    //文章id
    }

    //    php think baidu spider
    protected function execute(Input $input, Output $output)
    {

        $this->baseDomain = "https://www.gushiju.net/zidian";

        $this->ql = QueryList::getInstance();
        ini_set('max_execution_time', 600);//秒为单位，自己根据需要定义
        $method = $input->getArgument('method');
        $this->createHtmlRootDir = ROOT_PATH."public/";
        if($method == 'test'){
            $this->daoru();
        }

    }

    private function daoru(){
        $baiduKeywordmodel = Model('app\common\model\BaiduKeywords');
        $model = Model('app\common\model\Keywords');
        $rs = $model->where(['daoru'=>1])->limit(3000)->select();
        if(!empty($rs)){
            foreach($rs as $k=>$v){
                $tempData = $baiduKeywordmodel->where(['name'=>$v['name']])->find();
                if(empty($tempData)){
                    $dataSave = [
                      'name' => $v['name'],
                        'tag' => $v['tag'],
                        'status' => 1,
                        'is_use' => 0,
                    ];
                    $baiduKeywordmodel->insert($dataSave);
                    echo $baiduKeywordmodel->getLastSql()."\r\n\r\n";
                }
                $model->where(['id'=>$v['id']])->update(['daoru'=>2]);
                echo $model->getLastSql()."\r\n\r\n";
            }
        }
    }

}